﻿using IceDog.Uop.IRepositories;
using IceDog.Uop.Models.Context;
using IceDog.Uop.Repositories;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;

namespace IceDog.Uop.MainApiServer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(options =>
             {
                 options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
                 //HH:mm:ss 按照24小时制的格式进行字符串格式化
                 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
             });
            //配置mysql连接字符串
            //var connectionString = Configuration.GetConnectionString("MysqlConn");
            //services.AddDbContextPool<UopContext>(options => options.UseMySql(connectionString));

            var connectionString = Configuration.GetConnectionString("SqlLiteConn");
            services.AddDbContextPool<UopContext>(options => options.UseSqlite(connectionString));

            services.AddScoped<UserRepository>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
            app.UseStaticFiles();
            app.UseDefaultFiles(new DefaultFilesOptions() {
                DefaultFileNames=new List<string>() { "index.html" }
            });
        }
    }
}
